package com.lsnu.wzy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lsnu.wzy.po.Administrator;
import com.lsnu.wzy.po.Doctor;
import com.lsnu.wzy.po.Patient;
import com.lsnu.wzy.service.DepartMentService;
import com.lsnu.wzy.service.DoctorService;

@RestController  //相当于@Controller和@ResponseBody的一个结合,使该类中的方法直接返回字符串或者json数据给浏览器,而不是返回视图页面
@RequestMapping("/doctorOption")
public class DoctorController {
	
	@Autowired
	private  DoctorService doctorService;
	@Autowired
	private  DepartMentService departMentService;

	//搜索医生
	@RequestMapping("/findById/{dID}")
	public ModelAndView FindById(String dID)
	{
		List<Doctor> list = new ArrayList<Doctor>();
		Doctor doctor= doctorService.findDoctor(dID); 
		list.add(doctor);
		ModelAndView mav=new ModelAndView("/jsp/pages/doctors-list.jsp");
		mav.addObject("doctor", list);
		mav.addObject("dID",dID);
		return mav;		
	}
	
	//按照名字搜索医生
	@RequestMapping("/findByName/{dName}")
	public ModelAndView findByName(String dName)
	{
		List<Doctor> list = doctorService.findDoctorByName(dName); 
		ModelAndView mav=new ModelAndView("/jsp/index.jsp");
		mav.addObject("doctor", list);
		mav.addObject("dName",dName);
		return mav;		
	}
	
	//按照科室展示医生
	@RequestMapping("/findByDep/{department}")
	public ModelAndView findByDep(String department)
	{
		List<Doctor> list = doctorService.findByDep(department);
		ModelAndView mav=new ModelAndView("/jsp/index.jsp");
		mav.addObject("doctor", list);
		return mav;		
	}
	
	//管理员点击添加事件
	@RequestMapping("/beforeADD")
	public ModelAndView beforeADD()
	{
		Doctor doctor=doctorService.findLast();
		String dID=doctor.getdID();
		char[] arr=dID.toCharArray();
		StringBuilder builder = new StringBuilder();
		       for (int i = 0; i < arr.length; i++) {
//		       	如果字符的ACSLL码在48-57的范围内那肯定就是整数了
				if(arr[i] >= 48 && arr[i] <= 57) {
					builder.append(arr[i]);
				}
			}
		Integer integer=(Integer.parseInt(builder.toString()))+1;
		String dString="D0"+integer;
		ModelAndView mav=new ModelAndView("/doctorOption/addListen");
		mav.addObject("dID",dString);
		return mav;
	}
	
	//客户端浏览器发出的请求被封装成为一个HttpServletRequest对象。对象包含了客户端请求信息包括请求的地址，请求的参数，提交的数据，上传的文件客户端的ip甚至客户端操作系统都包含在其内。
	
	//添加医生
	@RequestMapping("/addDoctor")
	public void ADDDoctor(HttpServletRequest request,HttpServletResponse response,Doctor doctor) throws ServletException, IOException 
	{
		doctorService.addDoctor(doctor);
        request.getRequestDispatcher("/doctorOption/lsDoctor").forward(request,response);
	}
	
	//删除医生
	@RequestMapping("/delDoctor/{id}")
	  public void DELDoctor(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer id) throws ServletException, IOException 
	{
		doctorService.delDoctor(id);
	  request.getRequestDispatcher("/doctorOption/lsDoctor").forward(request,response); 
	}
	
	//URL中的 {xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。即获取的是请求路径中参数的值
	
	//编辑前的信息
	@RequestMapping("/editfind/{dID}")
	public ModelAndView FindById2(@PathVariable String dID)
	{
		List<Doctor> list = new ArrayList<Doctor>();
		Doctor doctor= doctorService.findDoctor(dID); 
		list.add(doctor);
		List<String> department= departMentService.LSDeName();
		System.out.println("编辑前医生信息："+list);
		ModelAndView mav=new ModelAndView("/jsp/pages/editDoctor.jsp");
		mav.addObject("doctor", list);
		mav.addObject("dID",dID);
		mav.addObject("department",department);
		return mav;		
	}
	
	//编辑医生信息
	@RequestMapping("/editDoctor/{id}")
	  public void EditDoctor(HttpServletRequest request,HttpServletResponse response, Doctor doctor) throws ServletException, IOException 
	{
		
		doctorService.editDoctor(doctor);
	    request.getRequestDispatcher("/doctorOption/lsDoctor").forward(request,response); 
	}
	 
	//全部医生
	@RequestMapping("/lsDoctor")
	public ModelAndView LSDoctor()
	{
		List<Doctor> doctorlist= doctorService.LSDoctor();
		ModelAndView mav=new ModelAndView();
		mav.addObject("doctor",doctorlist);
		mav.setViewName("/jsp/pages/doctors-list.jsp");
		return mav;		
	}
	
	
	//全部管理员
		@RequestMapping("/lsAdmin")
		public ModelAndView lsAdmin()
		{
			List<Administrator> doctorlist= doctorService.LSAdmin();
			ModelAndView mav=new ModelAndView();
			mav.addObject("admin",doctorlist);
			mav.setViewName("/jsp/pages/admin-list.jsp");
			return mav;		
		}
		
	//审核管理员
		@RequestMapping("/apply/{id}")
		public ModelAndView apply(@PathVariable Integer id)
		{
			doctorService.updateAdmin(id);
			ModelAndView mav=new ModelAndView();
			List<Administrator> doctorlist= doctorService.LSAdmin();
			mav.addObject("admin",doctorlist);
			mav.setViewName("/jsp/pages/admin-list.jsp");
			return mav;		
		}
		
		
	//主页展示医生信息
	@RequestMapping("/lsDoctor2")
	public ModelAndView LSDoctor2()
	{
		List<Doctor> doctorlist= doctorService.LSDoctor();
		ModelAndView mav=new ModelAndView();
		mav.addObject("doctor",doctorlist);
		mav.setViewName("/jsp/index.jsp");
		return mav;		
	}
	
	//主页搜索医生
	@RequestMapping("/findById2/{dID}")
	public ModelAndView FindDoctor(String dID)
	{
		List<Doctor> list = new ArrayList<Doctor>();
		Doctor doctor= doctorService.findDoctor(dID); 
		list.add(doctor);
		ModelAndView mav=new ModelAndView("/jsp/index.jsp");
		mav.addObject("doctor", list);
		mav.addObject("dID",dID);
		return mav;		
	}
	
	//登录
	@RequestMapping("/login")
	public void loginproving(HttpServletRequest request,HttpServletResponse response,Administrator administrator) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		Administrator ad = doctorService.loginproving(administrator.getAdminName(),administrator.getPassword());
		//System.out.println("调用验证方法+"+administrator);
		if(ad!=null)
		{
			session.setAttribute("administrator", ad);
			session.setAttribute("adminName", ad.getAdminName());//将管理员信息放到HttpSession对象
			request.getRequestDispatcher("/jsp/pages/main.jsp").forward(request,response); 
		}else {
			request.setAttribute("message", "登录信息错误");
			request.getRequestDispatcher("/html/login.html").forward(request,response);
		}
	}
	
	//下拉框的科室
	@RequestMapping("/addListen")
	public ModelAndView addListen()
	{
		List<String> department= departMentService.LSDeName();
		ModelAndView mav=new ModelAndView();
		mav.addObject("department",department);
		//System.out.println(department);
		mav.setViewName("/jsp/pages/addDoctor.jsp");
		return mav;		
	}
	
	//添加管理员
	@RequestMapping("/addAdmin")
	public void ADDAdmin(HttpServletRequest request,HttpServletResponse response,Administrator administrator) throws ServletException, IOException 
	{
		doctorService.addAdministrator(administrator);
	       request.getRequestDispatcher("/html/login.html").forward(request,response);
	}
	 
	
}
